<template>
  <div class="search-skill">
    <div class="top-box">
      <div class="nav-bar">
        <div class="nav-left">
          <i
            class="iconfont icon-zuojiantou"
            @click="$router.push('/share')"
          ></i>
        </div>
        <div class="nav-title">面经搜索</div>
      </div>
      <div class="search-box">
        <form action="/">
          <van-search
            v-model="value"
            show-action
            placeholder="请输入搜索关键词"
            shape="round"
            @search="onSearch"
            @cancel="onCancel"
            @clear="onCancel"
          />
        </form>
      </div>
    </div>

    <div v-if="searchList.length == 0" class="option-box">
      <div class="section">
        <div class="title">大家都在搜索</div>
        <ul class="tags">
          <li
            class="tag"
            @click="hotClick(item)"
            v-for="(item, index) in topShareSearch"
            :key="index"
          >
            {{ item }}
          </li>
        </ul>
      </div>
      <div class="section">
        <div class="title">历史记录<span @click="clear">清空</span></div>
        <ul class="tags">
          <li
            class="tag"
            @click="hotClick(item)"
            v-for="(item, index) in shareHistory"
            :key="index"
          >
            {{ item }}
          </li>
        </ul>
      </div>
    </div>

    <div v-else class="experience-list">
      <div @click="$router.push(`/sharedetail/${item.id}`)" v-for="item in searchList" :key="item.id" class="experience-item">
        <div class="exp-title">{{ item.title }}</div>
        <div class="exp-text">{{ item.content }}</div>
        <div class="exp-user">
          <div class="head">
            <img :src="baseURL + item.author.avatar" alt="" />
            <span>{{ item.author.nickname }}</span>
          </div>
          <div class="time">
            {{ formatTime(item.created_at) }}
          </div>
          <div class="prev">
            <van-icon name="comment-o" size="17px" /><span>{{
              item.article_comments
            }}</span>
          </div>
          <div class="zan">
            <van-icon
              name="good-job-o"
              size="17px"
              :class="{ active: item.star != 0 }"
            /><span>{{ item.star }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { topShareSearch, experienceList } from '@/api/skill'
export default {
  data () {
    return {
      value: '',
      topShareSearch: [],
      shareHistory: [],
      experienceList: [],
      searchList: []
    }
  },
  created () {
    this.loadtopShareSearch()
    this.loadexperienceList()
  },
  methods: {
    onSearch (val) {
      val = this.value
      this.experienceList.forEach((v) => {
        if (v.title.includes(val)) {
          this.searchList.push(v)
        }
      })
    },
    async loadexperienceList () {
      const res = await experienceList()
      this.experienceList = res.data.list
      console.log(this.experienceList)
    },
    formatTime (date) {
      return this.dayjs(date).format('YYYY.MM.DD')
    },
    onCancel () {
      this.searchList = []
    },
    async loadtopShareSearch () {
      if (localStorage.getItem('shareHistory')) {
        this.shareHistory = Object.values(
          JSON.parse(localStorage.getItem('shareHistory'))
        )
      }
      const res = await topShareSearch()
      this.topShareSearch = res.data
    },
    hotClick (value) {
      this.value = value
      if (!this.shareHistory.includes(value)) {
        this.shareHistory.push(value)
      }
      localStorage.setItem('shareHistory', JSON.stringify(this.shareHistory))
      this.onSearch()
    },
    clear () {
      this.shareHistory = []
      localStorage.removeItem('shareHistory')
    }
  }
}
</script>

<style lang="less" scoped>
.search-skill {
  .top-box {
    padding-bottom: 10px;
    padding-top: 5px;
    box-shadow: 0 0.05333rem 0.10667rem 0 rgb(24 26 57 / 4%);
    .nav-bar {
      position: relative;
      z-index: 1;
      display: flex;
      align-items: center;
      height: 40px;
      line-height: 1.5;
      text-align: center;
      background-color: #fff;
      .nav-left {
        position: absolute;
        top: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        padding: 0 10px;
        .iconfont {
          font-size: 25px;
        }
      }
      .nav-title {
        margin: 0 auto;
        font-size: 18px;
      }
    }
  }
  .option-box {
    padding: 12px 15px;
    .section {
      margin-bottom: 25px;
      .title {
        font-size: 17px;
        text-align: left;
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
          font-size: 12px;
        }
      }
      .tags {
        margin-top: 18px;
        display: flex;
        .tag {
          margin-right: 10px;
          margin-bottom: 6px;
          padding: 6px;
          border-radius: 4px;
          font-size: 12px;
          color: #545671;
          background-color: #eceaea;
        }
      }
      ul {
        list-style: none;
        margin: 0;
        padding: 0;
      }
    }
  }
  .experience-list {
    padding: 10px 15px;
    .experience-item {
      padding-top: 15px;
      padding-bottom: 20px;
      text-align: left;
      border-bottom: 1px solid #f8f9f9;
      .exp-title {
        font-size: 16px;
        margin-bottom: 7px;
      }
      .exp-text {
        margin-top: 10px;
        margin-bottom: 15px;
        font-size: 14px;
        display: -webkit-box;
        text-overflow: ellipsis;
        overflow: hidden;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
      .exp-user {
        display: flex;
        align-items: center;
        font-size: 13px;
        color: #b4b4bd;
        .head {
          flex: 1;
          display: flex;
          align-items: center;
          img {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 10px;
          }
        }
        .time {
          margin-right: 30px;
        }
        .prev,
        .zan {
          display: flex;
          align-items: center;
          margin-left: 15px;
          .active {
            color: red;
          }
          span {
            margin-left: 10px;
          }
        }
      }
    }
  }
}
</style>
